﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ingesup_ERP.Models;

namespace ingesup_ERP.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class ChargePlacementController : Controller
    {
        private db db = new db();

        //
        // GET: /Admin/ChargePlacement/

        public ViewResult Index()
        {
            return View(db.CHARGEDEPLACEMENT.ToList());
        }

        //
        // GET: /Admin/ChargePlacement/Details/5

        public ViewResult Details(string id)
        {
            CHARGEDEPLACEMENT chargedeplacement = db.CHARGEDEPLACEMENT.Single(c => c.LOGINPERS == id);
            return View(chargedeplacement);
        }

        //
        // GET: /Admin/ChargePlacement/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Admin/ChargePlacement/Create

        [HttpPost]
        public ActionResult Create(CHARGEDEPLACEMENT chargedeplacement)
        {
            if (ModelState.IsValid)
            {
                chargedeplacement.LOGINPERS = chargedeplacement.PRENOMPERS[0].ToString() + chargedeplacement.NOMPERS;
                chargedeplacement.MDPPERS = Functions.getHash(chargedeplacement.MDPPERS);
                db.CHARGEDEPLACEMENT.AddObject(chargedeplacement);
                db.SaveChanges();
                Roles.AddUserToRole(chargedeplacement.LOGINPERS, db.PersonneView.Single(m => m.LOGIN == chargedeplacement.LOGINPERS).TYPE);
                return RedirectToAction("Index");  
            }

            return View(chargedeplacement);
        }
        
        //
        // GET: /Admin/ChargePlacement/Edit/5
 
        public ActionResult Edit(string id)
        {
            CHARGEDEPLACEMENT chargedeplacement = db.CHARGEDEPLACEMENT.Single(c => c.LOGINPERS == id);
            return View(chargedeplacement);
        }

        //
        // POST: /Admin/ChargePlacement/Edit/5

        [HttpPost]
        public ActionResult Edit(CHARGEDEPLACEMENT chargedeplacement)
        {
            if (ModelState.IsValid)
            {
                db.CHARGEDEPLACEMENT.Attach(chargedeplacement);
                db.ObjectStateManager.ChangeObjectState(chargedeplacement, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(chargedeplacement);
        }

        //
        // GET: /Admin/ChargePlacement/Delete/5
 
        public ActionResult Delete(string id)
        {
            CHARGEDEPLACEMENT chargedeplacement = db.CHARGEDEPLACEMENT.Single(c => c.LOGINPERS == id);
            return View(chargedeplacement);
        }

        //
        // POST: /Admin/ChargePlacement/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            CHARGEDEPLACEMENT chargedeplacement = db.CHARGEDEPLACEMENT.Single(c => c.LOGINPERS == id);
            db.CHARGEDEPLACEMENT.DeleteObject(chargedeplacement);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}